<?php

class MenuWidget extends CWidget{
	
	public $instance;

	public function run(){
		Yii::trace("Running MenuWidget", "menu.MenuWidget");
		$this->render('menu', $this->getArguments());
	}
	
	private function getArguments(){
		Yii::import("menu.models.MenuItem");
		$menuItems = MenuItem::model()->renderMenu()->findAll('menu_id=:menu_id' ,array(':menu_id'=>$this->instance->id));
		foreach ($menuItems as $item) {
			Yii::trace("Checking if item is active: ".$item->path, "menu.MenuWidget");
			$item->setActive($this->isActive($item));
		}
		return array('items'=>$menuItems);
	}
	
	private function isActive($item){
		Yii::trace("Route: ".$this->getController()->getRoute(), "menu.MenuWidget");
		if($this->getController()->getRoute()!=$item->path){
			return false;
		}
		Yii::trace("Checking the action parameters", "menu.MenuWidget");
		$array_diff = array_diff($this->getController()->getActionParams(), $item->getParams());
		if(!empty($array_diff)){
			return false;
		}
		return true;
	}
	
}
